package com.rightsidetech.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.system.dtos.request.*;
import com.rightsidetech.system.dtos.response.material.ActivityFileResp;
import com.rightsidetech.system.dtos.response.material.ActivityMaterialAppResp;
import com.rightsidetech.system.dtos.response.material.ActivityMaterialDetailsResp;
import com.rightsidetech.system.dtos.response.material.ActivityMaterialResp;
import com.rightsidetech.system.entity.SysMaterialDetails;
import com.rightsidetech.system.entity.SysMaterial;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.mapper.SysMaterialDetailsMapper;
import com.rightsidetech.system.mapper.SysMaterialSendRecordMapper;
import com.rightsidetech.system.mapper.SysUserLoginInfoMapper;
import com.rightsidetech.system.service.SysMaterialService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Package: com.rightsidetech.system.service.impl
 * @ClassName: SysActivityMaterialServiceImpl
 * @Author: yinj
 * @CreateTime: 2021/12/13 9:57
 */
@Service("sysMaterialServiceImpl")
public class SysMaterialServiceImpl implements SysMaterialService {

    @Resource
    private SysMaterialSendRecordMapper sysMaterialSendRecordMapper;
    @Resource
    private SysUserLoginInfoMapper sysUserLoginInfoMapper;
    @Resource
    private SysMaterialDetailsMapper sysMaterialDetailsMapper;

    @Override
    public ResponseData<PageInfo<ActivityMaterialResp>> page(ActivityMaterialReq request) {
        PageHelper.startPage(request.getPageNo(), request.getPageSize());
        List<ActivityMaterialResp> list = sysMaterialSendRecordMapper.list(request);
        if (!CollectionUtils.isEmpty(list)) {
            for (ActivityMaterialResp resp : list) {
                List<ActivityFileResp> fileResps = sysMaterialDetailsMapper.selectByActivityId(resp.getId());
                if (!CollectionUtils.isEmpty(fileResps)) {
                    resp.setFileResps(fileResps);
                }
            }
        }
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData add(ActivityMaterialAddReq request, Long loginInfoId) {
        SysMaterial material = new SysMaterial();
        material.setTitle(request.getTitle());
        material.setTarget(request.getTarget());
        material.setCreatedAt(new Date());
        material.setCreatedId(loginInfoId);
        SysUserLoginInfo loginInfo = sysUserLoginInfoMapper.selectByPrimaryKey(loginInfoId);
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        material.setCreaterName(loginInfo.getLoginName());
        sysMaterialSendRecordMapper.insertSelective(material);
        if (!CollectionUtils.isEmpty(request.getFileReqs())) {
            sysMaterialDetailsMapper.insertBatch(request.getFileReqs(), material.getId());
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(ActivityMaterialUpdateReq request, Long loginInfoId) {
        SysMaterial sysActivityMaterial = sysMaterialSendRecordMapper.selectByPrimaryKey(request.getId());
        if (ObjectUtils.isEmpty(sysActivityMaterial)) {
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }

        SysMaterial material = new SysMaterial();
        material.setId(request.getId());
        material.setTitle(request.getTitle());
        material.setTarget(request.getTarget());
        material.setUpdaterId(loginInfoId);
        material.setUpdatedAt(new Date());
        sysMaterialSendRecordMapper.updateByPrimaryKeySelective(material);
        if (!CollectionUtils.isEmpty(request.getFileReqs())) {
            //逐条更新 多余逻辑删除
            List<SysMaterialDetails> list = sysMaterialDetailsMapper.selectList(request.getId());
            if (CollectionUtils.isEmpty(list)) {
                sysMaterialDetailsMapper.insertBatch(request.getFileReqs(), request.getId());
            }else {
                List<ActivityFileReq> fileReqs = request.getFileReqs();
                Iterator<SysMaterialDetails> listIterator = list.iterator();
                while (listIterator.hasNext()){
                    SysMaterialDetails details = listIterator.next();
                    Iterator<ActivityFileReq> reqIterator = fileReqs.iterator();
                    while (reqIterator.hasNext()){
                        ActivityFileReq fileReq = reqIterator.next();
                        if (details.getUrl().equals(fileReq.getUrl())){
                            reqIterator.remove();
                            listIterator.remove();
                            break;
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(list)){
                    List<Long> ids = list.stream().map(SysMaterialDetails::getId).collect(Collectors.toList());
                    sysMaterialDetailsMapper.updateByIds(ids);
                }
                if (!CollectionUtils.isEmpty(fileReqs)){
                    sysMaterialDetailsMapper.insertBatch(fileReqs, request.getId());
                }
            }
        }

        return ResponseData.success();
    }

    @Override
    public ResponseData operate(ActivityMaterialOperateReq request, Long loginInfoId) {
        SysMaterial sysActivityMaterial = sysMaterialSendRecordMapper.selectByPrimaryKey(request.getId());
        if (ObjectUtils.isEmpty(sysActivityMaterial)) {
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }

        if (request.getOperateType() == 1) {
            if (sysActivityMaterial.getStatus() == 1) {
                return ResponseData.fail("不能重复发送");
            }
            SysMaterial material = new SysMaterial();
            material.setId(request.getId());
            material.setStatus((byte) 1);
            material.setUpdaterId(loginInfoId);
            material.setUpdatedAt(new Date());
            sysMaterialSendRecordMapper.updateByPrimaryKeySelective(material);
        }
        if (request.getOperateType() == 2) {
            SysMaterial material = new SysMaterial();
            material.setId(request.getId());
            material.setIsDeleted((byte) 1);
            material.setUpdaterId(loginInfoId);
            material.setUpdatedAt(new Date());
            sysMaterialSendRecordMapper.updateByPrimaryKeySelective(material);
            sysMaterialDetailsMapper.updateDel(request.getId());
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData<ActivityMaterialDetailsResp> details(Long id) {
        ActivityMaterialDetailsResp resp = sysMaterialSendRecordMapper.findDetails(id);
        if (ObjectUtils.isEmpty(resp)) {
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        List<ActivityFileResp> list = sysMaterialDetailsMapper.selectByActivityId(resp.getId());
        resp.setFiles(list);
        return ResponseData.success(resp);
    }

    @Override
    public ResponseData<PageInfo<ActivityMaterialAppResp>> agentAppPage(PageRequest request, Byte target) {
        PageHelper.startPage(request.getPageNo(), request.getPageSize());
        List<ActivityMaterialAppResp> list = sysMaterialSendRecordMapper.listByTarget(target);
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData<List<ActivityFileResp>> fileDetails(Long id) {

        List<ActivityFileResp> list = sysMaterialDetailsMapper.selectByActivityId(id);
        return ResponseData.success(list);
    }
}
